home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Night Owl 6
/
Night Owl's Shareware - PDSI-006 - Night Owl Corp (1990).iso
/
039a
/
d3d.zip
/
SPHERE.C
< prev
next >
Wrap
Text File
|
1989-11-24
|
2KB
|
70 lines
/* SPHERE: m slices, n points on each circle */
#include <stdio.h>
#include <math.h>
#include <process.h>
main()
{
FILE *fp;
int i, j, m, n, nr, next, southpole;
double r, theta, phi, pi, delphi, deltheta, rsinphi, rcosphi, x, y, z;
char str[30];
pi = 4.0*atan(1.0);
printf("Enter m, the number of horizontal slices: "); scanf("%d", &m);
printf("\nThere will be n points on each horizontal circle.\n");
printf("It is recommended to choose n about twice as large an m.");
printf("\nEnter n: "); scanf("%d", &n);
delphi = pi/m;
deltheta = 2*pi/n;
printf("Radius of the sphere: "); scanf("%lf",&r);
printf("Name of the output file: "); scanf("%s",str);
if((fp = fopen(str, "w")) == NULL) {
printf("File problems");
exit(1);
}
/* Vertex numbering:
i = 0: 1
i = 1: 2, 3, ..., 2n+1
i = 2: n+2, n+3, ..., 2n+1
...
i=m-1: (m-2)n+2, (m-2)n+3, ..., (m-1)n+1
i=m: (m-1)n+2
*/
fprintf(fp, "%d %f %f %f\n", 1, 0.0, 0.0, r); /* i = 0 */
for(i=1; i<m; i++) {
phi = i*delphi;
rcosphi = r*cos(phi);
rsinphi = r*sin(phi);
for(j=0; j<n; j++) {
nr = (i-1)*n + j + 2;
theta = j*deltheta;
x = rsinphi * cos(theta);
y = rsinphi * sin(theta);
z = rcosphi;
fprintf(fp, "%d %f %f %f\n", nr, x, y, z);
}
}
fprintf(fp, "%d %f %f %f\n", (m-1)*n+2, 0.0, 0.0, -r);
fprintf(fp, "Faces:\n");
for(j=2; j<=n+1; j++)
fprintf(fp, "%d %d %d.\n", 1, j, (j<n+1 ? j+1 : 2));
for(i=1; i<m-1; i++) {
nr = (i-1)*n;
for(j=2; j<=n+1; j++) {
next = (j<n+1 ? j+1 : 2);
fprintf(fp, "%d %d %d %d.\n", nr+j, nr+n+j, nr+n+next, nr+next);
}
}
southpole = (m-1)*n + 2;
nr = (m-2)*n;
for(j=2; j<n+1; j++)
fprintf(fp, "%d %d %d.\n", nr+j, southpole, (j<n+1 ? nr+j+1 : nr + 2));
fclose(fp);
}